package com.ctrl.riveradministrator.mvp.view.activity.trace.search;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cazaea.sweetalert.SweetAlertDialog;
import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BaseListAdapter;
import com.ctrl.riveradministrator.mvp.model.Task;
import com.ctrl.riveradministrator.mvp.model.TraceSearch;
import com.ctrl.riveradministrator.mvp.presenter.TraceSearchPresenter;
import com.ctrl.riveradministrator.mvp.view.activity.river_doc.RiverDocActivity;
import com.ctrl.riveradministrator.mvp.view.activity.river_info.RiverDetailActivity;
import com.ctrl.riveradministrator.mvp.view.activity.river_policy.RiverPolicyActivity;
import com.ctrl.riveradministrator.mvp.view.activity.trace.TraceActivity;
import com.ctrl.riveradministrator.mvp.view.weight.BottomDialog;

import java.util.ArrayList;
import java.util.List;

public class TraceSearchActivity extends BaseActivity<TraceSearchPresenter> implements TraceSearchView,
        BaseListAdapter.OnItemClickListener<Task>, BottomDialog.BottomDiaItemOnClickListener {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private RecyclerView traceRv;
    private SwipeRefreshLayout traceSrl;
    private LinearLayoutManager layoutManager;
    private BottomDialog bottomDialog;
    private TraceSearchAdapter traceSearchAdapter;
    private Task task;
    private SweetAlertDialog sweetAlertDialog;
    private List<Task> model = new ArrayList<>();
    private int currentPage = 0;

    @Override
    protected void initParams(Bundle parms) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_trace_search;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        leftMenu = $(R.id.left_iv);
        traceRv = $(R.id.trace_list);
        traceSrl = $(R.id.trace_srl);
        traceSearchAdapter = new TraceSearchAdapter(this);
        layoutManager = new LinearLayoutManager(this);
        traceRv.setLayoutManager(layoutManager);
        traceSrl.setColorSchemeColors(Color.RED, Color.BLUE);
        traceSrl.setEnabled(false);
        traceRv.setAdapter(traceSearchAdapter);

        bottomDialog = new BottomDialog(this);

        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("加载中...");
        pDialog.setCancelable(false);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(getResources().getString(R.string.river_query));
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
        setSupportActionBar(toolbar);
    }

    @Override
    protected void doBusiness(Context mContext) {
        mPresenter.searchTrace("" + currentPage, "10");
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        traceSearchAdapter.setOnItemClickListener(this);
        bottomDialog.setListener(this);
        traceRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (traceRv.computeVerticalScrollOffset() > 0 &&
                            traceRv.computeVerticalScrollExtent() + traceRv.computeVerticalScrollOffset()
                                    >= traceRv.computeVerticalScrollRange()) {
                        currentPage = currentPage + 1;
                        mPresenter.searchTrace("" + currentPage, "10");
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected TraceSearchPresenter createPresenter() {
        return new TraceSearchPresenter(this);
    }

    @Override
    public void onSuccess(List<Task> model) {
        this.model.addAll(model);
        traceSearchAdapter.updateListView(this.model);
    }

    @Override
    public void onFailed(String message) {

    }

    @Override
    public void onItemClick(Task task, int position) {
        this.task = task;
        bottomDialog.showBottomTraceSearchDialog(task.getPatrolName());
    }

    @Override
    public void bottomDiaItem(int is) {
        switch (is) {
            case 0:
                startActivity(new Intent(this, DetailActivity.class).putExtra("task", task));
                break;
            case 1:
                Toast.makeText(this, "有待开发", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                startActivity(new Intent(this, TraceActivity.class)
                        .setAction("query")
                        .putExtra("patrolId", task.getPatrolId()));
                break;
        }
        bottomDialog.cancel();
    }

    @Override
    public void showLoading() {
        super.showLoading();
        //pDialog.show();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        //pDialog.cancel();
    }

}
